<?php 
namespace api\home\controller;
use cmf\controller\RestBaseController;
use think\Db;
class ApiController extends RestBaseController{
	public $data;
	public function __construct(){
		$this->data = input();
	}
	//预约诊查
	public function indagation(){
		$this->get_server(1);
	}
	//陪诊服务
	public function escort(){
		$this->get_server(2);
	}
	//预约服务数据
	//status字段说明 1上班 2休假 3调整
	//参数说明 
	//1. biz_id 模板绑定id
	//  下面的是搜索条件 可为空 （下面的查询均为真实查询 不支持模糊查询）
	//2. doc_name 医生姓名
	//3. poiid 门诊部id
	//4. time 搜索时间戳 默认本周时间戳  传值  搜索周 时间戳
	//5. kname 科室名称
	//6. page 页数
	private function get_server($type){
		if(@$this->data['time']){
			$time = $this->data['time'];
		}else{
			$time = time(); 
		}
		//本周时间
		$times[] = strtotime(date('Y-m-d', strtotime("this week Monday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Tuesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Wednesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Thursday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Friday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Saturday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Sunday", $time)));
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		$data_type = $type;//预约数据

		// 门诊查询
		$menzheng = Db::table('hospitalpoi')->where(['biz_id'=>$this->data['biz_id']])->select();
		if(!$menzheng){
			$this->error('请先添加门诊部门！');
		}
		$where1 = ' 1 = 1 ';

		// 检测是否存在 门诊选择
		if(@$this->data['poiid']){
			$poiid = $this->data['poiid'];
		}else{
			//选择第一条数据为默认数据
			$poiid = $menzheng[0]['poiid'];
		}
		// 检测是否输入医生查询
		if(@$this->data['dname']){
			$where1 .= ' and name = \''.$this->data['dname'].'\''; 
		}

		// 检测是否输入科室
		if (@$this->data['kname']) {
			// 科室查询
			$hospital_office = Db::table('hospital_office')->where(['biz_id'=>$this->data['biz_id']])->
				where(' name = \''.$this->data['name'].'\'')->find();
			if (!$hospital_office) {
				$this->error('该科室不存在！');
			}
			$ksid = $hospital_office['id'];
		}

		// 查询门诊部所在科室 中的医生
		$doctor_ids = Db::table('hospital_office')->where(['biz_id'=>$this->data['biz_id']])->
			where(' poi_id like\'%,'.$poiid.'%\'')->find();
		if(!$doctor_ids){
			$this->error('门诊部中没有医生！');
		}

		// 医生id拼接
		$doctor_id_str = '0'.$doctor_ids['doctor_id'].'0';


		// 对医生进行分页
		if(!isset($this->data['page'])||empty($this->data['page'])||$this->data['page']<=0){
			$this->data['page'] = 1;
		}
		$page = $this->data['page'];
		$toCount = Db::table('doctor')->where(['biz_id'=>$this->data['biz_id']])->where($where1.' and id in('.$doctor_id_str.') ')->count();
		if($toCount<=0){
			$this->success('没有数据！');
		}
		$toPage = ceil($toCount/10);

		// 查询医生数据
		$doctor_data = Db::table('doctor')->where(['biz_id'=>$this->data['biz_id']])->where($where1.' and id in('.$doctor_id_str.') ')->limit(($page-1)*10,10)->select();

		if(!$doctor_data){
			$this->error('请先添加医生！');
		}
		$where2 = ' and 1 = 1 ';
		if (@$ksid) {
			// 加入科室查询
			$where2 .= ' and off_id='.$ksid;
		}

		$server_data = Db::table('doctor_server_data')->
			where(['data_type'=>$data_type,'biz_id'=>$this->data['biz_id']])->
			where(' date_time >= '.$times['0'].' and date_time <= '.$times['6'].$where2)->select();
		$doctor_server_data = [];
		foreach ($doctor_data as $key => $val) {
			$doc_id = $val['id'];
			if (count($server_data)>0) {
				foreach ($server_data as $k => $v) {
					//生成对应编号
					$no = mb_substr( "7123456",date("w",$v['date_time']),1);
					//查询固定值班
					$fixed_duty = Db::table('doctor_server_fixed_data')->
						where(['biz_id'=>$this->data['biz_id'],'doc_id'=>$doc_id,'no'=>$no,'off_id'=>$v['off_id'],'poi_id'=>$v['poi_id']])->
						find();
					$val['server_data'][] = [
						'date_time' => $v['date_time'],
						'data_type' => $v['data_type'],
						'status' => $v['status'],
						'adjust_duty' => @unserialize($v['adjust_duty'])==false?'':unserialize($v['adjust_duty']),
						'fixed_duty' => @unserialize($fixed_duty)==false?'':unserialize($fixed_duty)
					];
				}
				//小于7  差数据
				if (count($val['server_data'])<7) {
					foreach ($val['server_data'] as $k1 => $v1) {
						foreach ($times as $k2 => $v2) {
							if($v2 == $v1['date_time']){
								unset($times[$k2]);
							}
						}
					}
					//写入漏掉的数据
					foreach ($times as $k3 => $v3) {
						$val['server_data'][] = [
							'date_time' => $v3,
							'data_type' => $data_type,
							'status' => 1,
							'adjust_duty' => '',
							'fixed_duty' => ''
						];
					}
				}
			}else{
				//没有数据插入时间 以及空数据
				foreach ($times as $key => $value) {
					$val['server_data'][] = [
						'date_time' => $value,
						'data_type' => $data_type,
						'status' => 1,
						'adjust_duty' => '',
						'fixed_duty' => ''
					];
				}
			}
			$doctor_server_data[] = $val;
		}
		$data['mengzhen'] = $menzheng;
		$data['doctor_server_data'] = $doctor_server_data;
		$data['toPage'] = $toPage;
		$data['toCount'] = $toCount;
		$this->success('success',$data);
	}
	//预约诊查 固定值班
	public function get_fixed_duty(){
		$this->duty(1);
	}
	//陪诊服务 固定值班
	public function get_adjust_duty(){
		$this->duty(2);
	}
	//固定值班  每个医生有且只能有7条数据 对应周一到周天
	//	参数 
	//	biz_id
	//	doc_id
	//	查询
	//	poiid 门诊id  默认第一个
	//	no 星期 1 2 3 4 5 6 7  默认1 星期1
	//	
	//	返回说明
	//	taketime 生效时间
	//	updatetime 更新时间
	//	fixed_duty 值班、号源 信息
	private function duty($type){
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		$menzheng = Db::table('hospitalpoi')->where(['biz_id'=>$this->data['biz_id']])->select();
		if(!$menzheng){
			$this->error('请先添加门诊部门！');
		}
		// 检测是否存在 门诊选择
		if(@$this->data['poiid']){
			$poiid = $this->data['poiid'];
		}else{
			//选择第一条数据为默认数据
			$poiid = $menzheng[0]['poiid'];
		}
		// 是否带星期N
		if(@$this->data['no']){
			$no = $this->data['no'];
		}else{
			$no = 1;
		}
		// 查询生效数据
		$list = Db::table('doctor_server_fixed_data')->
			where([
				'biz_id'=>$this->data['biz_id'],
				'poi_id'=>$poiid,
				'doc_id'=>$this->data['doc_id'],
				'data_type'=>$type,
				'no'=>$no
			])->
			find();
		$data['menzheng'] = $menzheng;
		$data['list'] = '';
		if (!$list) {
			$this->success('success',$data);
		}

		// 检测数据是否应该更新了
		if(@$list['taketime'] && @$list['new_duty']){
			if ($list['taketime'] >= mktime(0,0,0,date('m'),1,date('Y'))) { 
				// 数据需要自动更新了
				$list['fixed_duty'] = $list['new_duty'];
				$list['new_duty'] = '';
				$list['status'] = 1;
				Db::table('doctor_server_fixed_data')->update($list);
				$this->duty($type);
			}
		}
		$fixed_duty = $list['new_duty'];
		unset($list['new_duty']);
		unset($list['status']);
		$list['fixed_duty'] = @unserialize($fixed_duty) == false?'':unserialize($fixed_duty);
		$data['list'] = $list;
		$this->success('success',$data);
	}
	//添加 更新 预约诊查 固定值班
	public function update_fixed_duty(){
		$this->duty_update(1);
	}
	//添加 更新 陪诊服务 固定值班
	public function update_adjust_duty(){
		$this->duty_update(2);
	}
	// 值班数据详情
	private function duty_update($type){
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		if (!isset($this->data['poi_id'])||empty($this->data['poi_id'])) {
			$this->error('缺少参数[poi_id]');
		}
		if (!isset($this->data['no'])||empty($this->data['no'])) {
			$this->error('缺少参数[no]');
		}
		$this->data['fixed_duty'] = serialize($this->data['fixed_duty']);
		if (isset($this->data['id'])&&!empty($this->data['id'])) {
			$this->data['updatetime'] = time();
			$this->data['new_duty'] = serialize($this->data['fixed_duty']);
			$this->data['taketime'] = mktime(0,0,0,date('m'),1,date('Y'));
			unset($this->data['fixed_duty']);
			unset($this->data['biz_id']);
			unset($this->data['doc_id']);
			unset($this->data['poi_id']);
			unset($this->data['no']);
			Db::table('doctor_server_fixed_data')->
				where([
					'data_type'=>$type,
					'id'=>$this->data['id'],
					'biz_id'=>$this->data['biz_id'],
					'doc_id'=>$this->data['doc_id'],
					'poi_id'=>$this->data['poi_id'],
					'no'=>$this->data['no']
				])->
				update($this->data);
			$this->success('更新成功！');
		}else{
			// 添加后立即生效
			$this->data['addtime'] = time();
			$this->data['taketime'] = time();
			$this->data['data_type'] = $type;
			$this->data['new_duty'] = serialize($this->data['fixed_duty']);
			Db::table('doctor_server_fixed_data')->insert($this->data);
			$this->success('添加成功！');
		}
	}


	//预约调班列表
	public function y_tiaoban(){
		$this->duty_list(1);
	}
	//陪诊调班列表
	public function p_tiaoban(){
		$this->duty_list(2);
	}

	// 调班列表
	public function duty_list($type){
		$time = time();
		//本周时间
		$times[] = strtotime(date('Y-m-d', strtotime("this week Monday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Tuesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Wednesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Thursday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Friday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Saturday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("this week Sunday", $time)));
		
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Monday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Tuesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Wednesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Thursday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Friday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+0 week Saturday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Sunday", $time)));
		
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Monday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Tuesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Wednesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Thursday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Friday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+1 week Saturday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Sunday", $time)));
		
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Monday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Tuesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Wednesday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Thursday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Friday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+2 week Saturday", $time)));
		$times[] = strtotime(date('Y-m-d', strtotime("+3 week Sunday", $time)));

		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		$menzheng = Db::table('hospitalpoi')->where(['biz_id'=>$this->data['biz_id']])->select();
		if(!$menzheng){
			$this->error('请先添加门诊部门！');
		}
		// 检测是否存在 门诊选择
		if(@$this->data['poiid']){
			$poiid = $this->data['poiid'];
		}else{
			//选择第一条数据为默认数据
			$poiid = $menzheng[0]['poiid'];
		}

		foreach ($times as $key => $value) {
			$keys = $key+1;
			$keys = $keys%7;
			if ($keys == 0) {
				$keys = 7;
			}
			//固定值班数据写入
			$fixed_duty_data = Db::table('doctor_server_fixed_data')->
						where(['data_type'=>$type,'biz_id'=>$this->data['biz_id'],'doc_id'=>$this->data['doc_id'],'no'=>$keys,'poi_id'=>$this->data['poi_id']])->
						find();
			$list[$key][]['time'] = $value;
			$list[$key][]['fixed_duty'] = unserialize($fixed_duty_data['fixed_duty']) == false?[]:unserialize($fixed_duty_data['fixed_duty']);
			if (!$list) {
				$list[$key][]['fixed_duty'] = [];
			}
			//调班数据写入
			$lists = Db::table('doctor_server_data')->
				where(['data_type'=>$type,'biz_id'=>$this->data['biz_id'],'doc_id'=>$this->data['doc_id'],'poi_id'=>$this->data['poi_id'],'date_time'=>$value])->
				find();
			if (!$lists) {
				$list[$key][]['adjust_duty'] = [];
				$list[$key][]['status'] = 1;
			}else{
				$list[$key][]['adjust_duty'] = unserialize($lists['adjust_duty']) == false?[]:unserialize($lists['adjust_duty']);
				$list[$key][]['status'] = $lists['status'];
			}
		}
		$this->success('success',$list);
	}
	//预约请假
	public function yleave(){
		$this->leave(1);
	}
	//陪诊请假
	public function pleave(){
		$this->leave(2);
	}
	//请假
	private function leave($type){
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		if (!isset($this->data['poi_id'])||empty($this->data['poi_id'])) {
			$this->error('缺少参数[poi_id]');
		}
		if (!isset($this->data['date_time'])||empty($this->data['date_time'])) {
			$this->error('缺少参数[date_time]');
		}
		if (Db::table('doctor_server_data')->where([
			'biz_id'=>$this->data['biz_id'],
			'doc_id'=>$this->data['doc_id'],
			'poi_id'=>$this->data['poi_id'],
			'date_time'=>$this->data['date_time'],
			'data_type'=>$type
		])->find()) {
			Db::table('doctor_server_data')->where([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type
			])->update(['status'=>2]);
		}else{
			Db::table('doctor_server_data')->insert([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type,
				'status'=>2,
				'addtime'=>time()
			]);
		}
		$this->success('更新成功！');
	}
	//预约调班数据更新
	public function ytiaoban(){
		$this->adjustupdate(1);
	}
	//陪诊调班数据更新
	public function ptiaoban(){
		$this->adjustupdate(2);
	}
	//调班数据更新
	private function adjustupdate($type){
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		if (!isset($this->data['poi_id'])||empty($this->data['poi_id'])) {
			$this->error('缺少参数[poi_id]');
		}
		if (!isset($this->data['date_time'])||empty($this->data['date_time'])) {
			$this->error('缺少参数[date_time]');
		}
		if (!isset($this->data['adjust_duty'])||empty($this->data['adjust_duty'])) {
			$this->error('调班数据不能为空！');
		}
		if (Db::table('doctor_server_data')->where([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type
		])->find()) {
			Db::table('doctor_server_data')->where([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type
			])->update([
				'adjust_duty'=>serialize($this->data['adjust_duty']),
				'status'=>3
			]);
		}else{
			Db::table('doctor_server_data')->insert([
				'adjust_duty'=>serialize($this->data['adjust_duty']),
				'status'=>3,
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type,
				'addtime'=>time()
			]);
		}
		$this->success('更新成功！');
	}
	//预约请假数据恢复
	public function yhuifu(){
		$this->huifu(1);
	}
	//陪诊请假数据恢复
	public function phuifu(){
		$this->huifu(2);
	}
	//请假恢复
	public function private($type){
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['doc_id'])||empty($this->data['doc_id'])) {
			$this->error('缺少参数[doc_id]');
		}
		if (!isset($this->data['poi_id'])||empty($this->data['poi_id'])) {
			$this->error('缺少参数[poi_id]');
		}
		if (!isset($this->data['date_time'])||empty($this->data['date_time'])) {
			$this->error('缺少参数[date_time]');
		}
		if (Db::table('doctor_server_data')->where([
			'biz_id'=>$this->data['biz_id'],
			'doc_id'=>$this->data['doc_id'],
			'poi_id'=>$this->data['poi_id'],
			'date_time'=>$this->data['date_time'],
			'data_type'=>$type
		])->find()) {
			Db::table('doctor_server_data')->where([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type
			])->update(['status'=>1]);
		}else{
			Db::table('doctor_server_data')->insert([
				'biz_id'=>$this->data['biz_id'],
				'doc_id'=>$this->data['doc_id'],
				'poi_id'=>$this->data['poi_id'],
				'date_time'=>$this->data['date_time'],
				'data_type'=>$type,
				'status'=>1,
				'addtime'=>time()
			]);
		}
		$this->success('更新成功！');
	}
}